﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using Model;
using System.Data;

namespace DAL
{
   public  class QuestionsDAO
    {
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("Id", "T_Questions");
        }

        public bool Exists(int Id)
        {
            //StringBuilder strSql = new StringBuilder();
            //strSql.Append("select count(1) from WeiXinRequest");
            //strSql.Append(" where ID=@ID ");
            //SqlParameter[] parameters = {
            //        new SqlParameter("@ID", SqlDbType.Int,4)};
            //parameters[0].Value = ID;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Questions");
            strSql.Append("where Id=@Id");
            SqlParameter[] parameters = { new SqlParameter("@Id", Id) };
            //parameters[0].Value = Id;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        public int Add(Questions quesmodel)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Questions(");
            strSql.Append("Title,Option1,Option2,Option3,Option4)");
            strSql.Append(" values (");
            strSql.Append("@Title,@Option1,@Option2,@Option3,@Option4)");
            //strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Title", quesmodel.Title),
					new SqlParameter("@Option1",quesmodel.Option1),
					new SqlParameter("@Option2", quesmodel.Option2),
					new SqlParameter("@Option3", quesmodel.Option3),
					new SqlParameter("@Option4", quesmodel.Option4)
					
					};


            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" from  T_Questions ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where  " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}
